﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace WinFormClient
{
    public static class LocalStorage
    {
        static ClientData data;
        static void CheckLoad()
        {
            if (data == null)
            {
                Stream stream = new FileStream("LocalStorage.dat", FileMode.OpenOrCreate,
                    FileAccess.Read, FileShare.None);
                try
                {
                    IFormatter formatter = new BinaryFormatter();                    
                    data = (ClientData)formatter.Deserialize(stream);
                }
                catch (Exception ex)
                {
                    data = new ClientData();
                }
                finally {stream.Close(); }
            }
        }

        public static List<int> GetFocusedTopics()
        {
            CheckLoad();
            List<int> list = new List<int>(data.FocusedTopics.Keys);
            return list;
        }

        public static int SaveFocusedTopics(List<int> list)
        {
            foreach (int topicID in list) data.FocusedTopics.Add(topicID, "");
            AnytimeSave();
            return 0;
        }

        public static List<int> GetFocusedGroups()
        {
            CheckLoad();
            List<int> list = new List<int>(data.FocusedGroups.Keys);
            return list;
        }

        public static int SaveFocusedGroups(List<int> list)
        {
            foreach (int groupID in list) data.FocusedGroups.Add(groupID, "");
            AnytimeSave();
            return 0;
        }

            public static UserInfo GetUserInfo()
            {
                CheckLoad();
                return data.userinfo;
            }

            public static int SaveUserInfo(UserInfo info)
            {
                data.userinfo = info;
                AnytimeSave();
                return 0;
            }

            static void AnytimeSave() { SaveAll(); }
            public static void SaveAll()
            {
                IFormatter formatter = new BinaryFormatter();
                Stream stream = new FileStream("LocalStorage.dat", FileMode.Create,
                FileAccess.Write, FileShare.None);
                formatter.Serialize(stream, data);
                stream.Close();
            }

            public static ClientData Load()
            {
                Stream stream = new FileStream("LocalStorage.dat", FileMode.OpenOrCreate,
                    FileAccess.Read, FileShare.None);
                try
                {
                    IFormatter formatter = new BinaryFormatter();
                    data = (ClientData)formatter.Deserialize(stream);
                }
                catch (Exception ex)
                {
                    data = new ClientData();
                }
                finally { stream.Close(); }
                return data;
            }
            public static void Save(ClientData data)
            {
                IFormatter formatter = new BinaryFormatter();
                Stream stream = new FileStream("LocalStorage.dat", FileMode.Create,
                FileAccess.Write, FileShare.None);
                formatter.Serialize(stream, data);
                stream.Close();
            }
    }
}
